﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
   public class TaiKhoanDangNhapNVDAO
    {
        public static TaiKhoanDangNhapNV GetByID(int maID)
        {
            TaiKhoanDangNhapNV tk = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TaiKhoanDangNhapNV_GetById", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maTK", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                tk = new TaiKhoanDangNhapNV();
                tk.MaDangNhap = maID;
                tk.MatKhau = Convert.ToString(dataReader["MatKhau"]);
                tk.NgayDangNhap = Convert.ToDateTime(dataReader["NgayDangNhap"]);
                tk.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
            }
            cn.Close();
            return tk;
        }


        public static TaiKhoanDangNhapNV LayChiTietNguoiDung(int maTK, string matKhau)
        {
            TaiKhoanDangNhapNV tk = null;

            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TaiKhoanDangNhapNV_Authentic", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maDangNhap", maTK));
            cmd.Parameters.Add(new SqlParameter("@MatKhau", matKhau));
            SqlDataReader dataReader = cmd.ExecuteReader();

            if (dataReader.Read())
            {
                tk = new TaiKhoanDangNhapNV();
                tk.MaDangNhap = maTK;
                tk.MatKhau = matKhau;
                tk.NgayDangNhap = Convert.ToDateTime(dataReader["NgayDangNhap"]);
                tk.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
            }
            cn.Close();
            return tk;
        }

        public static void ThayDoiMatKhau(int maTK, string matKhau)
        {
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TaiKhoanDangNhapNV_ChangePassword", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maDangNhap", maTK));
            cmd.Parameters.Add(new SqlParameter("@newPass", matKhau));
            cmd.ExecuteNonQuery();
            cn.Close();
        }
    }
}
